package com.d3.sx_eprotection.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.d3.sx_eprotection.common.Result;
import com.d3.sx_eprotection.entity.*;
import com.d3.sx_eprotection.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/administrator")
public class AdministratorController {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private AdministratorService administratorService;
    @Autowired
    private SupervisorMesgService supervisorMesgService;
    @Autowired
    private SupervisorService supervisorService;

    @Autowired
    private CityService cityService;

    @Autowired
    private GriderService griderService;
    @Autowired
    private AQIlevelService aqIlevelService;
    @Autowired
    private GriderTestService griderTestService;
    @PostMapping("/getMesgList")
    public List<Map<String,Object>> getList(@RequestBody FyMesg fyMesg){
        Map<String, Object> conditionMap = new HashMap<>();
        List<Map<String,Object>> list=new ArrayList<>();
        if (fyMesg.getMessage().get("province")!=null){
        conditionMap.put("province",fyMesg.getMessage().get("province"));}
        if (fyMesg.getMessage().get("city")!=null){
        conditionMap.put("city",fyMesg.getMessage().get("city"));}
        if (fyMesg.getMessage().get("level")!=null){
        conditionMap.put("level",fyMesg.getMessage().get("level"));}
        if (fyMesg.getMessage().get("date")!=null){
        conditionMap.put("date",fyMesg.getMessage().get("date"));}
        if (fyMesg.getMessage().get("status")!=null){
        conditionMap.put("status",fyMesg.getMessage().get("status"));}
        Page<SupervisorMesg> page=new Page<>(fyMesg.getPageNum(), fyMesg.getPageSize());
        IPage<SupervisorMesg> mes= supervisorMesgService.returnList(page,conditionMap);
        for(SupervisorMesg mesg:mes.getRecords()){
            Map<String,Object> result = new HashMap<>();
            result.put("id",mesg.getId());
            result.put("name",supervisorService.getById(mesg.getSupervisor_phone()).getName());
            result.put("province",cityService.find(mesg.getProvince()).getName());
            result.put("city",cityService.findCity(mesg.getCity()).getName());
            result.put("date",mesg.getDate());
            result.put("time",mesg.getTime());
            switch (mesg.getLevel()){
                case 1:
                    result.put("level","优（一）");
                    break;
                case 2:
                    result.put("level","良（二）");
                    break;
                case 3:
                    result.put("level","轻度污染（三）");
                    break;
                case 4:
                    result.put("level","中度污染（四）");
                    break;
                case 5:
                    result.put("level","重度污染（五）");
                    break;
                case 6:
                    result.put("level","严重污染（六）");
                    break;
            }
            list.add(result);
        }
        return list;
    }
    //根据信息id获取信息完整属性
    @GetMapping("/getchoose")
    public Map<String,Object> getchoose(int id){
        SupervisorMesg get = supervisorMesgService.findone(id).get(0);
        Supervisor supervisor = supervisorService.getById(get.getSupervisor_phone());
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("name",supervisor.getName());
        map.put("sex",supervisor.getSex());
        map.put("date",get.getDate());
        map.put("phone",supervisor.getPhoneNumber());
        map.put("province",cityService.find(get.getProvince()).getName());
        map.put("city",cityService.findCity(get.getCity()).getName());
        map.put("address",get.getAddress());
        map.put("message",get.getMessage());
        map.put("level",get.getLevel());
        map.put("date",get.getDate());
        map.put("time",get.getTime());
        //用于确定是否有指派选项
        map.put("status",get.getStatus());
        return map;
    }
    @GetMapping("/getProvince")
    public List<Province> getList(){
        return cityService.getList();
    }
    @GetMapping("/getCityList")
    public List<City> getCityList(@RequestParam(required = false) Integer id){
        if (id==null){
            return cityService.listAll();
        }else {return cityService.query(id);}

    }
    @GetMapping("getLevelList")
    public List<AQIlevel> getLevelList(){
       return aqIlevelService.getList();
    }
    //排除非异地指派的城市
    @GetMapping("getCity")
    public List<City> getCity(int id,int cid){
        return  cityService.queryNot(id,cid);
    }
    //查询指定城市的网格员
    @GetMapping("/getGriders")
    public  List<Grider> getGriders(int id,int cid){
        return griderService.queryList(id,cid);
    }
    //更新公众监督信息的所属网格员以及status
    @GetMapping("/updateMesg")
    public Boolean update(int mesgId,String gid){
        return supervisorMesgService.updateGrider(mesgId,gid);
    }
    //查询已经被网格员确定过的信息列表（status=3）
    @GetMapping("/getFinished")
    public List<SupervisorMesg> getFinish(){
        return supervisorMesgService.getFinish();
    }

    @GetMapping("/getProvinceMesg")
    public List<Map<String,Object>> getProvinceMesg(){
        String sql = "SELECT * FROM province_data";
        List<Map<String,Object>> result = jdbcTemplate.queryForList(sql);
        return result;
    }

    @GetMapping("/getlevelCount")
    public List<Map<String,Object>> getLevelCount(){
        String sql = "SELECT * FROM level_count";
        List<Map<String,Object>> result = jdbcTemplate.queryForList(sql);
        return result;
    }

    @GetMapping("/getMonthCount")
    public List<Map<String,Object>> getMonthCount(){
        String sql = "SELECT * FROM month_level_count";
        List<Map<String,Object>> result = jdbcTemplate.queryForList(sql);
        return result;
    }

    @GetMapping("/getOtherMesg")
    public Map<String,Object> getOthers(){
        Map<String,Object> result =new HashMap<>();
        result.put("test_total",griderTestService.countMesg());
        result.put("well_total",griderTestService.countWell());
        result.put("capital_cover",(double)griderTestService.countCapital()/34*100);
        result.put("big_city_cover",(double)griderTestService.countBigCity()/619*100);
        return result;
    }

}
